<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.productSn" clearable placeholder="产品SN" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select
          v-model="query.productCode"
          clearable
          filterable
          placeholder="产品型号"
          style="width: 185px"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in productModel"
            :key="item.id"
            :label="item.productName"
            :value="item.productCode"
          >
            <span style="float: left">{{ item.productName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.productCode }}</span>
          </el-option>
        </el-select>
        <el-input v-model="query.orderCode" clearable placeholder="订单编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.workOrderCode" clearable placeholder="工单编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.operationCode" clearable placeholder="工序编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.deviceCode" clearable placeholder="设备编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.materialSn" clearable placeholder="批次号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.materialName" clearable placeholder="物料名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.materialCode" clearable placeholder="物料编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-date-picker
          class="filter-item"
          v-model="query.createTime"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="产品SN" prop="productSn">
            <el-input v-model="form.productSn" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品型号" prop="productCode">
            <el-input v-model="form.productCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单编号" prop="orderCode">
            <el-input v-model="form.orderCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工单编号" prop="workOrderCode">
            <el-input v-model="form.workOrderCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产线编号" prop="lienCode">
            <el-input v-model="form.lienCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工段编号" prop="sectionCode">
            <el-input v-model="form.sectionCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工位编号" prop="stationCode">
            <el-input v-model="form.stationCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工序编号" prop="operationCode">
            <el-input v-model="form.operationCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备编号" prop="deviceCode">
            <el-input v-model="form.deviceCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="物料编号" prop="materialSn">
            <el-input v-model="form.materialSn" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="物料名称" prop="materialName">
            <el-input v-model="form.materialName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="物料编码" prop="materialCode">
            <el-input v-model="form.materialCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="使用数量" prop="quantity">
            <el-input v-model="form.quantity" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="物料单位" prop="materialUnit">
            <el-input v-model="form.materialUnit" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="创建人">
            <el-input v-model="form.createBy" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="form.createTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="更新人">
            <el-input v-model="form.updateBy" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker v-model="form.updateTime" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="productSn" label="产品SN" width="250" align="center" />
        <el-table-column prop="productCode" label="产品型号" width="150" align="center" />

        <el-table-column prop="materialSn" label="批次码" width="250" align="center"  />
        <el-table-column prop="materialName" label="物料名称" width="150" align="center" />
        <el-table-column prop="materialCode" label="物料编码" width="150" align="center" />
        <el-table-column prop="quantity" label="使用数量" width="100" align="center" />
        <el-table-column prop="materialUnit" label="物料单位" width="100" align="center" />

        <el-table-column prop="operationCode" label="工序编号" width="150" align="center" />

        <el-table-column prop="productType" label="产品类型" width="150" align="center" />
        <el-table-column prop="orderCode" label="订单编号" width="150" align="center" />
        <el-table-column prop="workOrderCode" label="工单编号" width="150" align="center" />
        <el-table-column prop="lienCode" label="产线编号" width="100" align="center" />
        <el-table-column prop="sectionCode" label="工段编号" width="100" align="center" />
        <el-table-column prop="stationCode" label="工位编号" width="150" align="center" />
        <el-table-column align="center" width="150"  prop="shiftCode" label="班次编码" />

        <el-table-column prop="deviceCode" label="设备编号" width="150" align="center" />

        <el-table-column prop="createBy" label="创建人" width="100" align="center" />
        <el-table-column prop="createTime" label="创建时间" width="150" align="center" />
        <el-table-column prop="updateBy" label="更新人" width="100" align="center" />
        <el-table-column prop="updateTime" label="更新时间" width="150" align="center" />
        <el-table-column label="操作" width="150px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="toDelete(scope.row)">解绑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDataMaterialBatch from '@/api/data/dataMaterialBatch'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudProductModel from '@/api/product/productModel'

const defaultForm = { id: null, productSn: null, productCode: null, productType: null, orderCode: null, workOrderCode: null, lienCode: null, sectionCode: null, stationCode: null, operationCode: null, deviceCode: null, materialSn: null, materialName: null, materialCode: null, quantity: null, materialUnit: null, passStationId: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'DataMaterialBatch',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '物料批次', url: 'api/dataMaterialBatch', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDataMaterialBatch },optShow:{add: false,
        edit: false,
        del: false,
        download: true,
        reset: true,
        import: false}})
  },
  data() {
    return {
      productModel:[],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      permission: {
        add: ['admin', 'dataMaterialBatch:add'],
        edit: ['admin', 'dataMaterialBatch:edit'],
        del: ['admin', 'dataMaterialBatch:del']
      },
      rules: {
        productSn: [
          { required: true, message: '产品SN不能为空', trigger: 'blur' }
        ],
        productCode: [
          { required: true, message: '产品型号不能为空', trigger: 'blur' }
        ],
        productType: [
          { required: true, message: '产品类型不能为空', trigger: 'blur' }
        ],
        orderCode: [
          { required: true, message: '订单编号不能为空', trigger: 'blur' }
        ],
        workOrderCode: [
          { required: true, message: '工单编号不能为空', trigger: 'blur' }
        ],
        lienCode: [
          { required: true, message: '产线编号不能为空', trigger: 'blur' }
        ],
        sectionCode: [
          { required: true, message: '工段编号不能为空', trigger: 'blur' }
        ],
        stationCode: [
          { required: true, message: '工位编号不能为空', trigger: 'blur' }
        ],
        operationCode: [
          { required: true, message: '工序编号不能为空', trigger: 'blur' }
        ],
        deviceCode: [
          { required: true, message: '设备编号不能为空', trigger: 'blur' }
        ],
        materialSn: [
          { required: true, message: '物料编号不能为空', trigger: 'blur' }
        ],
        materialName: [
          { required: true, message: '物料名称不能为空', trigger: 'blur' }
        ],
        materialCode: [
          { required: true, message: '物料编码不能为空', trigger: 'blur' }
        ],
        quantity: [
          { required: true, message: '使用数量不能为空', trigger: 'blur' }
        ],
        materialUnit: [
          { required: true, message: '物料单位不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'productSn', display_name: '产品SN' },
        { key: 'productCode', display_name: '产品型号' },
        { key: 'productType', display_name: '产品类型' },
        { key: 'orderCode', display_name: '订单编号' },
        { key: 'workOrderCode', display_name: '工单编号' },
        { key: 'lienCode', display_name: '产线编号' },
        { key: 'sectionCode', display_name: '工段编号' },
        { key: 'stationCode', display_name: '工位编号' },
        { key: 'operationCode', display_name: '工序编号' },
        { key: 'deviceCode', display_name: '设备编号' },
        { key: 'materialSn', display_name: '物料编号' },
        { key: 'materialName', display_name: '物料名称' },
        { key: 'materialCode', display_name: '物料编码' },
        { key: 'materialUnit', display_name: '物料单位' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.params.productType='M';
      return true
    },
    toDelete(data) {
      this.$confirm("确定对该条码进行解绑?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.crud.doDelete(data);
        })
        .catch(() => {
        });
    },
  },
  mounted() {
    crudProductModel.list({productType: "M"}).then((data) => {
      this.productModel = data;
    });
  }
}
</script>

<style scoped>

</style>
